// pages/projectManagement.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userid: null,
    level: null,
    projectData: [],
    waitProjectData: [],
    finishProjectData: [],
    basicApprovalData: [],
    basicApprovedData: [],
    // 项目状态：进行中，已暂停，已终止，已过期
    statusText: '',
    currentData: 0,
    swiperHeight: 0,
    highTopTab: [{
        currentId: 0,
        name: '项目'
      },
      {
        currentId: 1,
        name: '待审核',
      },
      {
        currentId: 2,
        name: '已处理'
      }
    ],
    basicTopTab: [{
        currentId: 0,
        name: '项目列表',
      },
      {
        currentId: 1,
        name: '审批中'
      },
      {
        currentId: 2,
        name: '已审批'
      }
    ],
    showddList: false,
    basicList: [],
    basicDefName: '',
  },
  newProject: function() {
    // 新增项目
    wx.navigateTo({
      url: '/pages/newProject/newProject',
    })
  },
  openProjectDetail: function(e) {
    // 跳转项目详情
    let projectId = e.currentTarget.dataset.projectid
    let targetId = e.currentTarget.dataset.targetid
    let waitId = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/projectDetail/projectDetail?projectId=' + projectId + '&targetId=' + targetId+'&waitId='+waitId,
    })
  },
  getProjectData: function(userid) {
    // 获取项目列表数据
    let that = this
    let config = wx.getStorageSync('config')
    wx.request({
      url: config.host + 'api/project/findProject',
      method: 'GET',
      data: {
        userId: userid
      },
      success(res) {
        let data = res.data.data
        if (data.length > 0) {
          that.setData({
            projectData: data
          })
        }

      }
    })
  },
  swiperChange: function(e) {
    // 获取当前滑块
    let that = this
    that.setData({
      currentData: e.detail.current
    })
  },
  checkCurrent: function(e) {
    // 点击切换滑块
    let that = this
    if (that.data.currentData === e.target.dataset.current) {
      return false
    } else {
      that.setData({
        currentData: e.target.dataset.current
      })
    }
  },
  calcSwiperHeight: function() {
    // 计算swiper总高度
    let that = this
    wx.getSystemInfo({
      success: function(res) {
        let clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxr = 750 / clientWidth;
        let calc = clientHeight * rpxr
        that.setData({
          swiperHeight: calc
        })
      },
    })
  },
  showddList: function() {
    this.setData({
      showddList: !this.data.showddList
    })
  },
  getUserList: function() {
    // 获取自己和下级负责人
    let that = this
    let userid = wx.getStorageSync('userId')
    let config = wx.getStorageSync('config')
    wx.request({
      url: config.host + 'api/project/findSubordinate',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        userId: userid
      },
      success(res) {
        if (res.data.status == 200) {
          that.setData({
            basicList: res.data.data,
            basicDefName: res.data.data[0].name
          })
        }
      }
    })
  },
  clickBasicName: function(e) {
    // 选中负责人
    let that = this
    let basicId = e.currentTarget.dataset.basicid
    let name = e.currentTarget.dataset.name
    that.setData({
      basicDefName: name,
      showddList: !that.data.showddList,
      userid: basicId
    })
    // 重新加载项目数据
    that.getProjectData(basicId)
  },
  getWaitData: function() {
    // 获取待审核数据
    let that = this
    let userid = wx.getStorageSync('userId')
    let config = wx.getStorageSync('config')
    wx.request({
      url: config.host + 'api/project/findProjectReview',
      method: 'GET',
      data: {
        principalId: userid
      },
      success(res) {
        if (res.data.status == 200) {
          that.setData({
            waitProjectData: res.data.data
          })
        }
      }
    })
  },
  getfinishProjectData: function() {
    // 获取已处理数据
    let that = this
    let userid = wx.getStorageSync('userId')
    let config = wx.getStorageSync('config')
    wx.request({
      url: config.host + 'api/project/findProjectReviewTwo',
      method: 'GET',
      data: {
        principalId: userid
      },
      success(res) {
        if (res.data.status == 200) {
          that.setData({
            finishProjectData: res.data.data
          })
        }
      }
    })
  },
  basicApprovalData: function() {
    // 获取基层-审批中数据
    let that = this
    let userid = wx.getStorageSync('userId')
    let config = wx.getStorageSync('config')
    wx.request({
      url: config.host + 'api/project/findSubordinateReview',
      method: 'GET',
      data: {
        principalId: userid
      },
      success(res) {
        if (res.data.status == 200) {
          that.setData({
            basicApprovalData: res.data.data
          })
        }
      }
    })
  },
  basicApprovedData: function() {
    // 获取基层-已审批数据
    let that = this
    let userid = wx.getStorageSync('userId')
    let config = wx.getStorageSync('config')
    wx.request({
      url: config.host + 'api/project/findSubordinateTwo',
      method: 'GET',
      data: {
        principalId: userid
      },
      success(res) {
        if (res.data.status == 200) {
          that.setData({
            basicApprovedData: res.data.data
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this
    this.calcSwiperHeight()
    // 设置当前账号层级
    this.setData({
      level: wx.getStorageSync('level'),
      userid: wx.getStorageSync('userId')
    })
    if (this.data.level == 2) {
      // 获取下级负责人
      this.getUserList()
      // 获取项目数据
      this.getProjectData(this.data.userid)
      // 获取待审核数据
      this.getWaitData()
      // 获取已处理数据
      this.getfinishProjectData()
    } else if (this.data.level == 1) {
      // 获取项目数据
      this.getProjectData(this.data.userid)
      // 获取基层-审批中数据
      this.basicApprovalData()
      // 获取基层-已审批数据
      this.basicApprovedData()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (this.data.level == 2) {
      // 获取下级负责人
      this.getUserList()
      // 获取项目数据
      this.getProjectData(this.data.userid)
      // 获取待审核数据
      this.getWaitData()
      // 获取已处理数据
      this.getfinishProjectData()
    } else if (this.data.level == 1) {
      // 获取项目数据
      this.getProjectData(this.data.userid)
      // 获取基层-审批中数据
      this.basicApprovalData()
      // 获取基层-已审批数据
      this.basicApprovedData()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})